package com.example.hello;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class RateTask implements Runnable{
    private static final String TAG = "RateTask";
    Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        Log.i(TAG, "run: ......");
        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "title:" + doc.title());
        Element table = doc.getElementsByTag("table").get(1);
        Log.i(TAG, "table=" + table);
        Elements trs = table.getElementsByTag("tr");
        //List<String> list1=new ArrayList<String>();
        List<Rate> ret=new ArrayList<>();


        for (Element tr : trs) {
            Elements tds = tr.getElementsByTag("td");
            Log.i(TAG, "run:tds=" + tds);
            if (tds.size() > 0) {
                String str = tds.get(0).text();
                Log.i(TAG, "run: str=" + str);
                String val = tds.get(5).text();
                Log.i(TAG, "run: val=" + val);
                ret.add(new Rate(str,val));


                //list1.add(str + "->" + val);
            }
        }
        Message msg = handler.obtainMessage(11, ret);
        handler.sendMessage(msg);
    }
}
